/*
 * Copyright (c) 2008, University of Aarhus.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the name of the Univeristy of Aarhus nor the names
 *   of its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @author: Morten Tranberg Hansen <mth@cs.au.dk>
 * @date:   May 8 2008	
 * 
 */

package dk.sensobyg.basestation.remote;

import dk.sensobyg.basestation.*;
import java.util.*;
import java.io.*;
import java.rmi.*;

@Deprecated
public class GatewayClient {
	
	public static void main(String[] args) throws IOException, NotBoundException {
		System.out.println("Sensobyg Gateway Client Demo");
		
        if(args.length<2) {
        	System.out.println("	Usage: dk.sensobyg.basestation.remote.GatewayClient <host> <port>");
        	System.exit(1);
        }
        String host = args[0];
        int port = Integer.parseInt(args[1]);
		
		Gateway gateway = (Gateway) Naming.lookup("//" + host + ":" + port +"/SensoBygGateway");
		
		System.out.println("Connected to host at: " + host + ":" + port +"/SensoBygGateway");
		
		printHelp();
		
		BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) );
		boolean quit = false;
		while(!quit) {
		    String input = "";
		    input = console.readLine();
		    
		    if(input==null) continue;
		    
		    String[] parts = input.split(" ");
            String cmd = parts[0];
            
            if(cmd.equals("help")) {
            	printHelp();
            } else if(cmd.equals("quit") || cmd.equals("exit")) {
            	quit = true;
            } else if(cmd.equals("data")) {
            	String cond = "";
            	for(int i=1;i<parts.length;i++) {
            		if(!cond.equals(""))
            			cond += ",";
            		cond += parts[i];
            	}
            	try {
            		List<DataItem> dataList = gateway.getData(cond);
                	for(DataItem data : dataList) {
                		System.out.println(data);
                	}
            	} catch(RemoteException e) {
            		System.out.println(e);
            	}
            } else if(cmd.equals("motes")) {
            	try {
            		System.out.println(gateway.getMotes());
            	} catch(RemoteException e) {
            		System.out.println(e);
            	}
            } else if(cmd.equals("sample")) {
            	if(parts.length>1) {
            		try {
            			long interval = Long.parseLong(parts[1]);
            			System.out.println("Setting sample rate to " + interval);
                		Command command = new Command(Types.COMMAND_TYPE_TEMPERATURE, interval);
            			gateway.command(command);
            		} catch(NumberFormatException e) {
            			System.out.println(e);
            		} catch(RemoteException e) {
                		System.out.println(e);
                	}
            	}
            }
		}
	}

	public static void printHelp() {
		System.out.println("Availiable commands:");
		System.out.println("  help");
		System.out.println("  quit|exit");
		System.out.println("  data [<condition1>] [<condition2>] ... [<conditionN>]");
		System.out.println("  motes");
		System.out.println("  sample <interval>");
	}
}
